const { response } = require("express");
const express = require("express");
const { request } = require("http");
const app = express();
const path = require("path");

//处理body请求体的中间件
app.use(express.urlencoded())
app.use(express.json())


app.get('/user',(repuset,response)=>{
    const{userId}=request.query;
    if(userId !=='001'){
        return response.send({
            code: 10001,
            msg: "查询失败",
            data: nul
        })
    }
    response.send({
        code: 10000,
        msg: "查询成功",
        data: {
          name: "李博",
          age: 18,
          sex: "男"
        }
    })
})
app.get("/userInfo",(request,response)=>{
        const {userId}=request.query;
        if (userId !== "001") {
            return response.send({
              code: 10001,
              msg: "查询失败",
              data: null
            })
          } 
          //查询成功 响应
  response.send({
    code: 10000,
    msg: "查询成功",
    data: {
      name: "李博",
      age: 18,
      sex: "男"
    }
  }) 
})
app.post('/adduser',(requset,response)=>{
    const{
        name,
        age,
        sex
    }=request.body;
    if (!name || !age || !sex) {
        return res.send({
          code: 10001,
          mes: "添加失败",
          data: null
        })
      }
    
      response.send({
        code: 10000,
        mes: "添加成功",
        data: null
      })



})
//静态资源部署接口
app.get('/1',(requset,response)=>{
    const filePath = path.resolve(__dirname, "./01.ajax的一级封装.html");
    response.sendFile(filePath);
})
app.get('/2',(requset,response)=>{
    const filePath = path.resolve(__dirname, "./02.ajax的二级封装.html");
    response.sendFile(filePath);
})
app.get('/3',(requset,response)=>{
    const filePath = path.resolve(__dirname, "./03.ajax的三级封装.html");
    response.sendFile(filePath);
})
app.listen(3000, () => {
    console.log("服务器请求成功 请访问 http://127.0.0.1:3000");
  })